package com.oocl.oopdemo.sixday;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Date;

import org.junit.Ignore;
import org.junit.Test;

public class IOTest {

	@Ignore
	@Test
	public void testInputstreamBasic() throws IOException{
		long begin=System.currentTimeMillis();
		InputStream is=new FileInputStream("abc.txt");
//		System.out.println((char)is.read());
//		System.out.println((char)is.read());
//		System.out.println();
		int b=0;
		while((b=is.read())!=-1){
			System.out.println((char)b);
		}
		long end=System.currentTimeMillis();
		System.out.println("time:"+(end-begin));
	}
	@Ignore
	@Test
	public void testInputStreamBuffer() throws IOException{
//		InputStream is=new FileInputStream("abc.txt");
		InputStream is=new FileInputStream("map.jpg");
		
		byte[] buff=new byte[10000];
//		int i=is.read(buff);
//		int i1=is.read(buff);
//		System.out.println();
//		System.out.println(i);
//		System.out.println(i1);
//		System.out.println(new String(buff,0,5)+"hello");
	
		int length=0;
		while((length=is.read(buff))!=-1){
			System.out.println(new String(buff,0,length));
		}
		
	}
	@Ignore
	@Test
	public void testOutputStream() throws IOException{
		OutputStream os=new FileOutputStream("out.txt");
		os.write(56);
		os.write(53);
		os.close();
	}
	@Ignore
	@Test
	public void testOutputStream1() throws IOException{
		OutputStream os=new FileOutputStream("out1.txt");
		os.write("Hello ITA yuan".getBytes("utf-8"));
		os.write(53);
		System.out.println("hhe");
		os.close();
	}
	@Ignore
	@Test
	public void testPicture() throws IOException{
		InputStream is=new FileInputStream("map.jpg");
		OutputStream os=new FileOutputStream("map11.jpg");
		String[] msg=new String[]{"I","love","u","2017-07-11"};
		int length=-1;
		int index=0;
		byte[] buffer=new byte[1024*8];
		while((length=is.read(buffer))!=-1){
			os.write(buffer, 0, length);
			if(index>=2*length&&index%(4*length)==0
					&&index/(4*length)<msg.length){
				os.write((msg[index/(4*length)]).getBytes());
			}
			index +=length;
		}
		is.close();
		os.close();
	}
	@Ignore
	@Test
	public void testByteArray() throws IOException{
		InputStream is=new ByteArrayInputStream("asdfgg".getBytes());
		OutputStream os=new ByteArrayOutputStream();
		System.out.println(is.read());
		os.write(97);
		os.write(99);
		System.out.println(os);
		os.close();
	}
	@Ignore
	@Test
	public void testObjectStream() throws IOException{
		ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("objectout"));
		Date d=new Date();
		os.writeObject(d);
		os.close();
		
	}
	@Ignore
	@Test
	public void testObjectStream1() throws FileNotFoundException, IOException, ClassNotFoundException{
		ObjectInputStream is=new ObjectInputStream(new FileInputStream("objectout"));
		Date d=(Date)is.readObject();
		System.out.println(d.getTime());
		is.close();
	}
	@Ignore
	@Test
	public void testObjectStream2() throws FileNotFoundException, IOException, ClassNotFoundException{
		ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("objectout2"));
		User u=new User("hhe","qianng","hhhh");//User serializable
		os.writeObject(u);
		os.close();
	}
	@Ignore
	@Test
	public void testDataStream() throws IOException{
		DataOutputStream dos=new DataOutputStream(new FileOutputStream("dataout"));
		dos.writeDouble(4.7);
		dos.writeUTF("Hello");
		dos.close();
	}
	@Ignore
	@Test
	public void testDataStream1() throws IOException{
		DataInputStream dos=new DataInputStream(new FileInputStream("dataout"));
		System.out.println(dos.readDouble());
		System.out.println(dos.readUTF());
		dos.close();
	}
//	@Ignore
	@Test
	public void testBufferedInputStream() throws IOException{
		BufferedInputStream bis=new BufferedInputStream(new FileInputStream("testfile"),1024);
		BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("copyOfTestFile"),1024);
		int m=-1;
		while((m=bis.read())!=-1){
			bos.write(m);
		}
		bis.close();
		bos.close();
	}
}
